package com.example.pei.ithinklearnt.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.pei.ithinklearnt.AppApplication;
import com.example.pei.ithinklearnt.R;
import com.example.pei.ithinklearnt.adapter.base.BaseViewHolder;
import com.example.pei.ithinklearnt.adapter.base.OnPictureClickListener;
import com.example.pei.ithinklearnt.adapter.base.OnRecyclerViewListener;
import com.example.pei.ithinklearnt.bean.UserInfo;
import com.example.pei.ithinklearnt.listener.QueryUserListener;
import com.example.pei.ithinklearnt.model.UserModel;
import com.example.pei.ithinklearnt.view.activity.UserInfoActivity0;
import com.makeramen.roundedimageview.RoundedImageView;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

import java.text.SimpleDateFormat;

import butterknife.BindView;
import cn.bmob.newim.bean.BmobIMImageMessage;
import cn.bmob.newim.bean.BmobIMMessage;
import cn.bmob.newim.bean.BmobIMUserInfo;
import cn.bmob.v3.exception.BmobException;

import static com.example.pei.ithinklearnt.utils.ImageLoaderUtil.displayImage;
import static com.example.pei.ithinklearnt.utils.ImageLoaderUtil.getPhotoImageOption;
import static com.example.pei.ithinklearnt.utils.ImageLoaderUtil.getPhotoRoundImageOption;
import static com.example.pei.ithinklearnt.utils.MyUtils.getDensity;
import static com.example.pei.ithinklearnt.utils.MyUtils.getSendTime;

/**
 * 接收到的文本类型
 */


public class ReceiveImageHolder extends BaseViewHolder {

    @BindView(R.id.iv_avatar)
    RoundedImageView iv_avatar;

    @BindView(R.id.tv_time)
    TextView tv_time;

    @BindView(R.id.iv_picture)
    ImageView iv_picture;
    @BindView(R.id.progress_load)
    ProgressBar progress_load;

    private OnPictureClickListener onPictureClickListener;

    public ReceiveImageHolder(Context context, ViewGroup root, OnRecyclerViewListener onRecyclerViewListener,OnPictureClickListener onPictureClickListener) {
        super(context, root, R.layout.item_chat_received_image,onRecyclerViewListener);
        this.onPictureClickListener=onPictureClickListener;
    }

    @Override
    public void bindData(Object o) {
        BmobIMMessage msg = (BmobIMMessage)o;
        //用户信息的获取必须在buildFromDB之前，否则会报错'Entity is detached from DAO context'
        final BmobIMUserInfo info = msg.getBmobIMUserInfo();
        displayImage(AppApplication.getContext(), iv_avatar, info != null ? info.getAvatar():null, getPhotoImageOption());
        // ImageLoaderFactory.getLoader().loadAvator(iv_avatar,info != null ? info.getAvatar() : null, R.mipmap.head);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = dateFormat.format(msg.getCreateTime());
        tv_time.setText(getSendTime(time ));
        //可使用buildFromDB方法转化为指定类型的消息
        final BmobIMImageMessage message = BmobIMImageMessage.buildFromDB(false,msg);
        //显示图片


        displayImage(AppApplication.getContext(), iv_picture, message.getRemoteUrl(), getPhotoRoundImageOption(),new ImageLoadingListener(){

            @Override
            public void onLoadingFailed(String s, View view, FailReason failReason) {
                progress_load.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onLoadingCancelled(String s, View view) {
                progress_load.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onLoadingComplete(String s, View view, Bitmap bitmap) {
                progress_load.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onLoadingStarted(String s, View view) {
                progress_load.setVisibility(View.VISIBLE);
            }
        });
        //点击头像
        iv_avatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserModel.getInstance().queryUserInfo(info.getUserId(), new QueryUserListener() {
                    @Override
                    public void done(UserInfo s, BmobException e) {
                        if(e==null){
                            Bundle bundle=new Bundle();
                            bundle.putSerializable("u",s);
                            startActivity(UserInfoActivity0.class,bundle);
                        }
                    }
                });

            }
        });
        //点击图片信息
        iv_picture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               // toast("点击图片:"+message.getRemoteUrl()+"");
                if(onPictureClickListener!=null){
                    onPictureClickListener.onPictureClick(message.getRemoteUrl());
                }
                if(onRecyclerViewListener!=null){
                    onRecyclerViewListener.onItemClick(v,getAdapterPosition());
                }
            }
        });
        //长按图片信息
        iv_picture.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (onRecyclerViewListener != null) {
                    onRecyclerViewListener.onItemLongClick(v,getAdapterPosition());
                }
                return true;
            }
        });

    }

    public void showTime(boolean isShow) {
        tv_time.setVisibility(isShow ? View.VISIBLE : View.GONE);
    }
}